package org.springframework.samples.countries.web.translate;

import java.util.List;
import java.util.Stack;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import pikes.ecma.Assignment;
import pikes.ecma.IAssignmentExpression;
import pikes.ecma.IMemberExpression;
import pikes.ecma.MemberPropertyAccess;

public class PutField implements ByteCodeOperation {

	protected static final Log translatorLogger = LogFactory.getLog("Translator");

	private String name = null;

	public PutField(String name) {
		this.name = name;
	}

	public void execute(int numberOfParameters, Stack<Object> stack, List<String> localVariables) {

		IAssignmentExpression rightHandSide = (IAssignmentExpression) stack.pop();
		IMemberExpression memberExpression = (IMemberExpression) stack.pop();
		Assignment assignment = new Assignment(new MemberPropertyAccess(memberExpression, name), rightHandSide);
		stack.push(assignment);
		translatorLogger.info("PUTFIELD: Putting field '" + name + "'");
	}

	@Override
	public String toString() {
		return "putfield " + name;
	}

}
